package com.googlecode.dgwt.client.dijit.base.widget;

import com.google.gwt.user.client.Event;
import com.googlecode.dgwt.client.dijit.Widget;
import com.googlecode.dgwt.client.dijit.theme.Theme;

/**
 * @author wellsantos@wellsantos.com
 * @since 0.01
 */
public class WidgetHandler implements UIHandler {

    Widget widget;
    UIHandler nextHandler;
    
    
    public WidgetHandler(){
    }
    
    public WidgetHandler(WidgetHandler nextHandler){
        if (nextHandler != null){
            addHandler(nextHandler);
        }
    }
    
    
    public void addHandler(WidgetHandler nextHandler){
        assert (nextHandler != null);
        assert (nextHandler.nextHandler == null) : "nextHandler already attached to another engine";
            
        if (this.nextHandler == null){
            this.nextHandler = nextHandler;
        } else {
            nextHandler.nextHandler = this.nextHandler;
            this.nextHandler = nextHandler;
        }
    }
    
    public final Widget getWidget(){
        return widget;
    }
    
    public void initialize(){
        nextHandler.initialize();
    }
    
    public void buildRendering(){
        nextHandler.buildRendering();
    }

    public void onPostCreate(){
        nextHandler.onPostCreate();
    }
    
    public void onStartup(){
        nextHandler.onStartup();
    }
    
    public void onThemeChanging(Theme newTheme){
        nextHandler.onThemeChanging(newTheme);
    }

    public void onThemeChanged(Theme oldTheme){
        nextHandler.onThemeChanged(oldTheme);
    }

    public void onBrowserEvent(Event event){
        nextHandler.onBrowserEvent(event);
    }

    @Override
    public void doAttachChildren() {
        nextHandler.doAttachChildren();
    }

    @Override
    public void doDetachChildren() {
        nextHandler.doDetachChildren();
    }
    
}
